﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Manage_BefGoodsList : System.Web.UI.Page, HZ.MBSM.BLL.IPageDescribe
{
    public string name, medicinemodel, outlookc, strConditions;
    public string show;
    protected void Page_Load(object sender, EventArgs e)
    {


        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {

            if (!IsPostBack)
            {
                txtname.Text = "";
                txtmedicinemodel.Text = "";
                txtoutlook.Text = "";


                List_memo1.Items.Add("全部");
                List_memo1.Items.Add("非医保");
                List_memo1.Items.Add("医保");
                //chk1.Visible = false;
            }

            showGridView();
        }
    }

    protected void Button5_Click(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 查询总共有多少页
    /// </summary>
    /// <returns></returns>
    /// <summary>
    /// 将数据绑定到GridView1
    /// </summary>
    /// <param name="PageIndex">转向第几页</param>
    public void showGridView()
    {

        string _SelectField = "GoodsByCompanyid as goodsid,case GoodsByCompany.IsSendin when 1 then '已递交' else '未递交' end as IsSendin,productname,GoodsName,medicinemodel,outlookc,Unit,xishu,AccountName_SH,shenhetime,NoPass,case GoodsByCompany.CheckStatus when '1' then '审核通过' when '2' then '审核未通过' when '3' then '纸质材料一致' else '未审核' end as CheckStatus, case GoodsByCompany.memo1 when '0' then '非医保' when '1' then '医保' end as memo1,PackUnit,companyName,AccountName as AccountName_TJR,dbo.Chk_GetCompanyName(AccountId_TJR) as companyname_trj,case QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " end as QualityLevels";

        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "  GoodsByCompany left JOIN company ON GoodsByCompany.AccountId_Sc = company.AccountId inner join Account on GoodsByCompany.AccountId_TJR=Account.AccountId ";                                               //要查询的表明
        dbPage.Key = "GoodsByCompanyid";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = _SelectField;//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = Convert.ToInt32(Lpagenum.SelectedItem.Value);                                                    //没有显示记录条数   
        dbPage.OrderBy = "GoodsByCompanyid,productname,medicinemodel,outlookc";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        dr = null;
        db = null;
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键



    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp = "";

        string _memo1 = List_memo1.Text.ToString();

        switch (_memo1)
        {
            case "全部":
                break;
            case "非医保":
                strtemp += " GoodsByCompany.memo1='0' and";
                break;
            case "医保":
                strtemp += " GoodsByCompany.memo1='1' and";
                break;

        }
        //审核状态
        if (List_memo3.SelectedValue != "4")
        {
            strtemp += " isnull(GoodsByCompany.CheckStatus,0)='" + List_memo3.SelectedValue + "' and";
        }
        //审核人
        if (txtSHR.Text.Trim() != "")
        {
            strtemp += " AccountName_SH like '%" + txtSHR.Text.Trim() + "%' and";
        }
        //流水号
        string stgoods = txtgoods.Text.ToString().Trim();
        if (stgoods != "")
        {
            strtemp = strtemp + "  GoodsByCompanyid ='" + stgoods + "' and";
        }

        //通用名

        string name = txtname.Text.ToString().Trim();
        if (name != "")
        {
            strtemp = strtemp + "  ProductName like '%" + name + "%' and";
        }
        //剂型
        string medicinemodel = txtmedicinemodel.Text.ToString().Trim();
        if (medicinemodel != "")
        {
            strtemp = strtemp + " medicinemodel like '%" + medicinemodel + "%' and";
        }
        //规格
        string outlookc = txtoutlook.Text.ToString().Trim();
        if (outlookc != "")
        {
            strtemp = strtemp + " outlookc like '%" + outlookc + "%' and";
        }
        //商品名

        string strgoodsname = txtGoodsName.Text.ToString().Trim();
        if (strgoodsname != "")
        {
            strtemp = strtemp + " goodsname like '%" + strgoodsname + "%' and";
        }
        //添加人名称  或注册号
        string companyname = txtCompanyName.Text.ToString().Trim();
        if (companyname != "")
        {
            strtemp = strtemp + " (dbo.Chk_GetCompanyName(AccountId_TJR) like '%" + companyname + "%' or AccountName like '%" + companyname + "%') and";
        }
        //单位
        string strunit = TextUnit.Text.ToString().Trim();
        if (strunit != "")
        {
            strtemp += " unit like '%" + strunit + "%'  and";
        }
        ////企业注册号

        //string strCM = txtCompanyNum.Text.ToString().Trim();
        //if (strCM != "")
        //{
        //    strtemp += " Usercode like'%" + strCM + "%' and";
        //}
        //转换系数
        String strXS = txtZhuanhuan.Text.ToString().Trim();
        if (strXS != "")
        {
            strtemp += " xishu = '" + strXS + "' and";

        }
        //是否递交
        switch (LIsSendin.SelectedIndex)
        {
            case 0:// 未递交 
                strtemp += " isnull(GoodsByCompany.IsSendin,0)=0 and";
                break;
            case 1: //已递交
                strtemp += " isnull(GoodsByCompany.IsSendin,0)=1 and";
                break;
            case 2:   //全部

                break;

        }
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }

        return strtemp;

    }
    public string fctConditions2()
    {
        string strtemp = "";


        //流水号
        string stgoods = txtgoods.Text.ToString().Trim();
        if (stgoods != "")
        {
            strtemp = strtemp + "  a.GoodsByCompanyid ='" + stgoods + "' and";
        }

        //通用名
        string name = txtname.Text.ToString().Trim();
        if (name != "")
        {
            strtemp = strtemp + "  a.ProductName like '%" + name + "%' and";
        }
        //剂型
        string medicinemodel = txtmedicinemodel.Text.ToString().Trim();
        if (medicinemodel != "")
        {
            strtemp = strtemp + " a.medicinemodel like '%" + medicinemodel + "%' and";
        }
        //规格
        string outlookc = txtoutlook.Text.ToString().Trim();
        if (outlookc != "")
        {
            strtemp = strtemp + " a.outlookc like '%" + outlookc + "%' and";
        }
        //商品名

        string strgoodsname = txtGoodsName.Text.ToString().Trim();
        if (strgoodsname != "")
        {
            strtemp = strtemp + " a.goodsname like '%" + strgoodsname + "%' and";
        }
        //生产企业名称
        string companyname = txtCompanyName.Text.ToString().Trim();
        if (companyname != "")
        {
            strtemp = strtemp + " a.companyName like '%" + companyname + "%' and";
        }
        //单位
        string strunit = TextUnit.Text.ToString().Trim();
        if (strunit != "")
        {
            strtemp += " a.unit like '%" + strunit + "%'  and";
        }
        ////企业注册号

        //string strCM = txtCompanyNum.Text.ToString().Trim();
        //if (strCM != "")
        //{
        //    strtemp += " a.Usercode like'%" + strCM + "%' and";
        //}

        //转换系数
        String strXS = txtZhuanhuan.Text.ToString().Trim();
        if (strXS != "")
        {
            strtemp += " a.xishu = '" + strXS + "' and";
        }
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }

        return strtemp;

    }
    protected void ToEcxul_Click(object sender, EventArgs e)
    {

        Db db = new Db();
        System.Data.IDataReader dr;
        string _strSql = "Select a.goodsid as 流水号,a.productname as 通用名,a.GoodsName as 商品名,a.medicinemodel as 剂型,a.outlookc as 规格,a.factor as 转换系数,case ifbid when 0 then '否' else  '是' end as 是否确认投标,case a.QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " end as 质量层次 ,a.UserCode as 企业注册号 ,a.companyName as 生产企业,a.AccountName_TJR as 添加人,case a.CheckStatus when'0' then '未审核' when null then '未审核' when '1' then '审核通过' when '2' then '不通过' when '3' then '原始状态'  end as 审核状态,case projectid when '14' then '自行采购' when '13' then '招标目录' end as 目录,case a.companycheck when '0' then '未审核' when '1' then '审核通过' when '2' then '审核未通过' end as 企业审核状态,a.AccountName_SHR as 审核人,a.shenhetime as 审核时间 ,ISNULL(b.memo5,'') AS 联系人,ISNULL(b.Telepone,'') AS 电话 ,ISNULL(b.memo10,'') AS 手机 From   v_product_goods  as a LEFT OUTER JOIN Account ON a.AccountId_SH = Account.AccountId LEFT OUTER JOIN v_Company AS b on b.UserCode=a.usercode inner join bid_project_product as c on a.productid=c.productid  Where " + fctConditions2() + " Order By a.productname,a.medicinemodel,a.outlookc,a.companyName,a.goodsid";
        _strSql = "SELECT goodsid AS 流水号,productname AS 通用名,ProductNameSM AS 说明书名称,Medicinemodel AS 剂型,Outlookc AS 规格,Unit AS 单位,xishu AS 转换系数,GoodsName AS 商品名, companyname as 生产企业,LicenseNumber AS 批准文号,LicenseNumberDate AS 批准文号过期,SurveyReportId AS 质检报告,SurveyReportDate AS 质检报告过期,gmpcode AS GMP,gmpdate AS GMP过期,memo5 AS GMP综合信息,RetailPrice AS 物价,memo4 AS 零售价出处 , jzdate AS 零售价有效期 ,memo1 AS 医保类型,memo2 AS 药品类别,expr1 AS 目录类别,zxcg 招标分类 FROM dbo.v_product_goods WHERE CheckStatus=1";

        dr = db.DataReader(_strSql);
        GridView2.DataSource = dr;
        GridView2.DataBind();
        db.Close();
        dr.Close();
        dr.Dispose();
        string filename = System.Web.HttpUtility.UrlEncode("商品审核信息.xls");
        SysFun.WriteToExcel(GridView2, this.Page, filename);


    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //OverRide　为了使导出成Excel可行！

    }

    #region IPageDescribe 成员

    public string Name
    {
        get { return "审核商品信息"; }
    }

    public string Describe
    {
        get { return "审核商品信息"; }
    }

    public string ParentName
    {
        get { return "商品维护"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAdmin);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCheckGoods);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumComapanyRG);

            return _AccountType;
        }
    }
    #endregion


    // 确认递交
    protected void Button6_Click(object sender, EventArgs e)
    {
        Updateissendin(1);
    }
    //取消递交
    protected void Button7_Click(object sender, EventArgs e)
    {
        Updateissendin(0);
    }

    private void Updateissendin(int _issendin)
    {
        string _strgoods = "";
        if (Request.Form["chk"] != null && Request.Form["chk"].ToString() != "")
        {
            _strgoods = Request.Form["chk"].ToString();
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            HZ.MBSM.BLL.Goods good = new HZ.MBSM.BLL.Goods();
            if (good.UpdateIssendin(_strgoods, _Account.AccountId.ToString(), _issendin, 1))
            {
                Response.Write("<script>alert('提交成功！！！');</script>");
            }
            else
            {
                Response.Write("<script>alert('提交失败！！！');</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('请选择商品！！！');</script>");
        }
        showGridView();
    }
}
